#!/usr/bin/env python

# Author:	Erik Wallentinsen
# Version:	0 (draft)
# Modified:	27/05/2008

from addressbook import Contact
import addressbook

def printHelp():
	print
	print 'AddressBook Manager Terminal'
	print '==========================================================='
	print 'help : print this help message'
	print 'add  : add a contact to the address book'
	print 'get  : get information about a contact from the address book'
	print 'list : list all current contacts'
	print 'del  : delete a contact from the address book'
	print

def addValue():
	print 'Add information to this contact using the syntax key=value <newline>. End with <newline>'		
	a = raw_input('% ')
	data = {}
	while a != '':
		record = a.split('=')
		if len(record) == 2:
			data[record[0]] = record[1]
		else:
			print 'Invalid input'
		a = raw_input('% ')
	
	c = Contact(data)
	addressbook.addContact(c)
	print 'Added user with ID: ', c.id

def getContact():
	id = input('User ID: ')
	data = addressbook.getContact(id).data
	for key in data:
		print key, ": ", data[key]

def deleteContact():
	id = input('User ID: ')
	addressbook.deleteContact(id)

def listContacts():
	for contact in addressbook.getAllContacts():
		print '-' * 50
		print 'User ID: ', contact.id
		for key in contact.data:
			print key, ": ", contact.data[key] 

while True:
	cmd = raw_input("$ ")
	
	if cmd == 'help':
		printHelp()
		continue
	if cmd == 'add':
		addValue()
		continue
	if cmd == 'get':
		getContact()		
		continue
	if cmd == 'del' or cmd == 'delete':
		deleteContact()
		continue
	if cmd == 'list':
		listContacts()
		continue
	if cmd != '':
		print 'No such command: ', cmd
		printHelp()
